package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.Constants;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.PTUserInfoExcution;
import com.fortune.dto.in.pUser.PChangePasswordDto;
import com.fortune.dto.in.pUser.PLoginDto;
import com.fortune.dto.in.pUser.PResetPasswordDto;
import com.fortune.entity.PUserInfo;
import com.fortune.enums.PTUserInfoEnum;
import com.fortune.service.PPomissionService;
import com.fortune.service.PTUserInfoService;
import com.fortune.vo.pUser.LoginPUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "user", description = "平台员工账号相关", tags = {"平台员工账号相关"})
@RestController
@RequestMapping("pUser")
public class PTUserInfoController {
    @Autowired
    private PTUserInfoService ptUserInfoService;
    @Autowired
    private PPomissionService pPomissionService;
    @Autowired
    private RedisClient redisClient;

    @ApiOperation(value = "平台员工登录", nickname = "pUserLogin", notes = "", tags={ "@石匡" })
    @PostMapping(value = "login",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<LoginPUserInfo> login(HttpServletRequest request, @Validated @RequestBody PLoginDto loginDto) {
        int check = ptUserInfoService.loginCheck(loginDto);
        if (check==1) {
            return new PTUserInfoExcution<LoginPUserInfo>(null, PTUserInfoEnum.PASSWORDFALSE);
        }else if (check==2) {
            return new PTUserInfoExcution<LoginPUserInfo>(null, PTUserInfoEnum.PHONEFALSE);
        }else if (check == 3){
            return new PTUserInfoExcution<LoginPUserInfo>(null, PTUserInfoEnum.ACCOUNT_NOT);
        }else {
            PUserInfo pUserInfo = ptUserInfoService.pUserInfo(loginDto);
            HttpSession session = request.getSession();
            session.setAttribute(Constants.SESSION_P_USER, pUserInfo);
            String positonId = pUserInfo.getPpositionId();
            List<String> pomissions = pPomissionService.listPermissions(positonId);
            session.setAttribute(Constants.SESSION_PERMISSION, pomissions);
            LoginPUserInfo loginPUserInfo = ptUserInfoService.loginPUserInfo(loginDto);
            return new PTUserInfoExcution<LoginPUserInfo>(loginPUserInfo, PTUserInfoEnum.SUCCESS);
        }
    }

    @ApiOperation(value = "平台员工重置密码", nickname = "mPserResetPassword", notes = "", tags={ "@石匡" })
    @PostMapping(value = "password/reset",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> resetPassword(HttpServletRequest request,@Validated @RequestBody PResetPasswordDto resetPasswordDto) {
        int num = ptUserInfoService.queryPhoneExist(resetPasswordDto.getPhone());
        if (num < 1){
            return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.PHONE_NOT_EXIST);
        }
        String check = redisClient.get(CacheKeys.EMAIL_SEND_CODE, resetPasswordDto.getPhone());
        if (check==null || check.isEmpty()) {
            return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.SESSIONCODEFALSE);
        }
        if (check.equals(resetPasswordDto.getCode())) {
            int result = ptUserInfoService.resetPassword(resetPasswordDto);
            if (result < 1) {
                return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.FALSE);
            }
            return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.SUCCESS);
        }
        return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.CODEFALSE);
    }

    @ApiOperation(value = "平台员工退出登录", nickname = "pUserLogout", notes = "", tags={ "@石匡" })
    @PostMapping(value = "logout")
    public BaseAPIExcution<Void> logount(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return new PTUserInfoExcution<Void>(null,PTUserInfoEnum.SUCCESS);
    }


    @ApiOperation(value = "平台员工修改密码", nickname = "pUserChangePassword", notes = "", tags={ "@石匡" })
    @PostMapping(value = "password/change",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> changePassword(HttpServletRequest request,@Validated @RequestBody PChangePasswordDto changePasswordDto) {
        HttpSession session = request.getSession();
        PUserInfo pUserInfo = (PUserInfo) session.getAttribute(Constants.SESSION_P_USER);
        if (pUserInfo==null) {
            return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.NOLOGIN);
        }
        int result =ptUserInfoService.updatePassword(pUserInfo, changePasswordDto);
        if (result > 0) {
            return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.SUCCESS);
        }else{
            return new PTUserInfoExcution<Void>(null, PTUserInfoEnum.PASSWORDFALSE);
        }
    }


}
